#!/bin/sh

#
# Test the failure conditions of fat_cp
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
define(BOOT_PART_OFFSET, 63)
define(BOOT_PART_COUNT, 77238)

task no_file {
    on-init {
        fat_mkfs(\${BOOT_PART_OFFSET}, \${BOOT_PART_COUNT})
        fat_cp(\${BOOT_PART_OFFSET}, "doesntexist", "something")
    }
}
task corrupt_partition {
    on-init {
        fat_mkfs(\${BOOT_PART_OFFSET}, \${BOOT_PART_COUNT})
        fat_cp(1, "1k.bin", "1k-new.bin")
    }
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

echo "Expect to fail..."
if $FWUP_APPLY -q -d $IMGFILE -t no_file $FWFILE; then
    echo "Expected apply to fail due to missing the file, but it didn't."
    exit 1
fi
echo "Expect to fail..."
if $FWUP_APPLY -q -d $IMGFILE -t corrupt_partition $FWFILE; then
    echo "Expected apply to fail due to a corrupt filesystem, but it didn't."
    exit 1
fi

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
